home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / bstring / bstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-15  |  5.2 KB  |  229 lines

  1. /* 
  2.  * bstring.c --
  3.  *
  4.  *    This file contains a program that exercises the bstring
  5.  *    library procedures.  Invoke it with no parameters;  it
  6.  *    will print messages on stderr for any problems it detects
  7.  *    with the string procedures.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: /sprite/src/tests/bstring/RCS/bstring.c,v 1.2 88/12/15 09:07:52 ouster Exp $ SPRITE (Berkeley)";
  21. #endif not lint
  22.  
  23. #include <stdio.h>
  24. #include <bstring.h>
  25.  
  26. #define error(string) \
  27.     fprintf(stderr, string); \
  28.     exit(1);
  29.  
  30. int
  31. check(srcPtr, numBytes, start)
  32.     unsigned char *srcPtr;    /* First byte to check. */
  33.     int numBytes;        /* How many bytes to check. */
  34.     int start;            /* Bytes should have ascending values starting
  35.                    at start. */
  36. {
  37.     int i;
  38.     for (i = 0; i < numBytes; i++) {
  39.     if (srcPtr[i] != ((start+i)&0377)) {
  40.         return 0;
  41.     }
  42.     }
  43.     return 1;
  44. }
  45.  
  46. int
  47. checkZero(srcPtr, numBytes)
  48.     unsigned char *srcPtr;    /* First byte to check. */
  49.     int numBytes;        /* How many bytes to check. */
  50. {
  51.     int i;
  52.     for (i = 0; i < numBytes; i++) {
  53.     if (srcPtr[i] != 0) {
  54.         return 0;
  55.     }
  56.     }
  57.     if (srcPtr[numBytes] == 0) {
  58.     return 0;
  59.     }
  60.     return 1;
  61. }
  62.  
  63. main()
  64. {
  65.     int result = 0;
  66.     static unsigned char test1[400], test2[400], test3[400];
  67.     int i;
  68.  
  69.     for (i = 0; i < 400; i++) {
  70.     test3[i] = i;
  71.     }
  72.  
  73.     /*
  74.      * bcopy
  75.      */
  76.  
  77.     bcopy(test3, test1, 400);
  78.     if (!check(test1, 400, 0)) {
  79.     error("bcopy error 1\n");
  80.     }
  81.     bcopy(test1+16, test1+32, 127);
  82.     if (!check(test1+32, 127, 16)) {
  83.     error("bcopy error 2\n");
  84.     }
  85.     if ((test1[160] != 160) || (test1[31] != 31)) {
  86.     error("bcopy error 3\n");
  87.     }
  88.     bcopy(test3, test1, 400);
  89.     bcopy(test1+64, test1+16, 127);
  90.     if (!check(test1+16, 127, 64)) {
  91.     error("bcopy error 4\n");
  92.     }
  93.     if ((test1[144] != 144) || (test1[15] != 15)) {
  94.     error("bcopy error 5\n");
  95.     }
  96.     bcopy(test3, test1, 100);
  97.     bcopy(test1+1, test1+16, 32);
  98.     if (!check(test1+16, 32, 1)) {
  99.     error("bcopy error 6\n");
  100.     }
  101.     if ((test1[15] != 15) || (test1[48] != 48)) {
  102.     error("bcopy error 7\n");
  103.     }
  104.     bcopy(test3, test1, 100);
  105.     bcopy(test1+17, test1+8, 32);
  106.     if (!check(test1+8, 32, 17)) {
  107.     error("bcopy error 8\n");
  108.     }
  109.     if ((test1[7] != 7) || (test1[40] != 40)) {
  110.     error("bcopy error 9\n");
  111.     }
  112.     bcopy(test3, test1, 100);
  113.     bcopy(test1+16, test1+3, 32);
  114.     if (!check(test1+3, 32, 16)) {
  115.     error("bcopy error 10\n");
  116.     }
  117.     if ((test1[2] != 2) || (test1[35] != 35)) {
  118.     error("bcopy error 11\n");
  119.     }
  120.     bcopy(test3, test1, 100);
  121.     bcopy(test1+16, test1+33, 32);
  122.     if (!check(test1+33, 32, 16)) {
  123.     error("bcopy error 12\n");
  124.     }
  125.     if ((test1[32] != 32) || (test1[65] != 65)) {
  126.     error("bcopy error 13\n");
  127.     }
  128.     bcopy(test3, test1, 100);
  129.     bcopy(test1+2, test1+6, 64);
  130.     if (!check(test1+6, 64, 2)) {
  131.     error("bcopy error 14\n");
  132.     }
  133.     bcopy(test3, test1, 100);
  134.     bcopy(test1+6, test1+2, 64);
  135.     if (!check(test1+2, 64, 6)) {
  136.     error("bcopy error 15\n");
  137.     }
  138.  
  139.     /*
  140.      * bcmp
  141.      */
  142.  
  143.     bcopy(test3+1, test1, 200);
  144.     bcopy(test3+5, test2, 200);
  145.     test1[135] = 66;
  146.     if (bcmp(test1+8, test2+4, 127) != 0) {
  147.     error("bcmp error 1\n");
  148.     }
  149.     test1[134] = 20;
  150.     if (bcmp(test1+8, test2+4, 127) == 0) {
  151.     error("bcmp error 2\n");
  152.     }
  153.     test1[133] = 19;
  154.     if (bcmp(test1+6, test2+2, 127) != 0) {
  155.     error("bcmp error 3\n");
  156.     }    test1[132] = 18;
  157.     if (bcmp(test1+6, test2+2, 127) == 0) {
  158.     error("bcmp error 4\n");
  159.     }
  160.     if (bcmp(test1+5, test2+1, 127) != 0) {
  161.     error("bcmp error 5\n");
  162.     }
  163.     test1[131] = 17;
  164.     if (bcmp(test1+5, test2+1, 127) == 0) {
  165.     error("bcmp error 6\n");
  166.     }
  167.     test1[72] = 16;
  168.     if (bcmp(test1+8, test2+4, 64) != 0) {
  169.     error("bcmp error 7\n");
  170.     }
  171.     test1[71] = 15;
  172.     if (bcmp(test1+8, test2+4, 64) ==  0) {
  173.     error("bcmp error 8\n");
  174.     }
  175.     bcopy(test3+4, test1, 200);
  176.     if (bcmp(test1+4, test2+3, 64) != 0) {
  177.     error("bcmp error 9\n");
  178.     }
  179.     if (bcmp(test1+5, test2+4, 64) != 0) {
  180.     error("bcmp error 10\n");
  181.     }
  182.  
  183.     /*
  184.      * bzero
  185.      */
  186.  
  187.     bcopy(test3, test1, 200);
  188.     bzero(test1, 1);
  189.     if (!checkZero(test1, 1)) {
  190.     error("bzero error 1\n");
  191.     }
  192.     bcopy(test3, test1, 200);
  193.     bzero(test1, 8);
  194.     if (!checkZero(test1, 8)) {
  195.     error("bzero error 2\n");
  196.     }
  197.     bcopy(test3, test1, 200);
  198.     bzero(test1, 127);
  199.     if (!checkZero(test1, 127)) {
  200.     error("bzero error 3\n");
  201.     }
  202.     bcopy(test3, test1, 200);
  203.     bzero(test1+2, 127);
  204.     if (!checkZero(test1+2, 127)) {
  205.     error("bzero error 4\n");
  206.     }
  207.     bcopy(test3, test1, 200);
  208.     bzero(test1+3, 127);
  209.     if (!checkZero(test1+3, 127)) {
  210.     error("bzero error 5\n");
  211.     }
  212.  
  213.     /*
  214.      * ffs
  215.      */
  216.  
  217.     if (ffs(0) != -1) {
  218.     error("ffs error 1\n");
  219.     }
  220.     if (ffs(1) != 1) {
  221.     error ("ffs error 2\n");
  222.     }
  223.     if (ffs(1<<31) != 32) {
  224.     error ("ffs error 3\n");
  225.     }
  226.  
  227.     return result;
  228. }
  229.